package sv.gob.mh.dgii.common.io;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

public class FileLineReader implements Iterable<String>, Iterator<String>, Closeable {

    /**
     * Holds the next element (to be peeked)
     */
    public String next = null;
    /**
     * Working object
     */
    protected String currentLine = null;
    protected BufferedReader bufferedReader;

    public FileLineReader(File f) throws FileNotFoundException {
        bufferedReader = new BufferedReader(new FileReader(f));
    }

    public FileLineReader(String fileName) throws FileNotFoundException {
        bufferedReader = new BufferedReader(new FileReader(fileName));
    }

    public FileLineReader(InputStream is) {
        InputStreamReader isr = new InputStreamReader(is);
        bufferedReader = new BufferedReader(isr);
    }

    @Override
    public Iterator<String> iterator() {
        return this;
    }

    @Override
    public boolean hasNext() {
        next = readLine();
        return next != null;
    }

    @Override
    public String next() {
        String current;
        /*
         * si se llamo has next la linea a leer se encuentra en la variable next
         * sino se lee la linea del buffer
         * 
         */
        if (next == null) {
            current = readLine();
        } else {
            current = next;
            next = null;
        }

        currentLine = current;

        try {
            if (current == null) {
                close();
            }
        } catch (IOException e) {
            // This does not happen
            // If it does, nobody cares
        }
        return (current);
    }

    private String readLine() {
        String temp = null;
        try {
            temp = bufferedReader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return (temp);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Can't remove lines from a file with this class");
    }

    @Override
    public void close() throws IOException {
        bufferedReader.close();
    }
}
